package com.mysoft.gfyx.marketdevelop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mysoft.framework.common.util.MappingUtil;
import com.mysoft.framework.service.AppService;
import com.mysoft.gfyx.marketdevelop.dao.ProvinceandcityDao;
import com.mysoft.gfyx.marketdevelop.dao.entity.AdvanceSearchUserSaved;
import com.mysoft.gfyx.marketdevelop.dao.entity.Provinceandcity;
import com.mysoft.gfyx.marketdevelop.service.ProvinceAndCityService;
import com.mysoft.gfyx.marketdevelop.service.dto.ProvinceAndCityConditionDto;
import com.mysoft.gfyx.marketdevelop.service.dto.ProvinceAndCityDto;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * 描述：Provinceandcity 业务代码
 *
 * @author JavaSdk generator
 * @date 2022-07-22
 */
@Service
public class ProvinceAndCityServiceImp extends AppService implements ProvinceAndCityService {

    @Resource
    ProvinceandcityDao provinceandcityDao;

    @Override
    public List<ProvinceAndCityDto> getAll() {
        QueryWrapper<Provinceandcity> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("DisplayOrder");
        List<Provinceandcity> provinceandcityList = provinceandcityDao.selectList(queryWrapper);
        List<ProvinceAndCityDto> provinceAndCityDtoList = MappingUtil.getSimpleMapperFacade().mapAsList(provinceandcityList, ProvinceAndCityDto.class);
        return provinceAndCityDtoList;
    }

    @Override
    public List<ProvinceAndCityDto> getByCodes(List<String> codes) {
        QueryWrapper<Provinceandcity> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("DisplayOrder");
        if (codes.size() > 0) {
            queryWrapper.in("ItemCode", codes);
        }
        List<Provinceandcity> provinceandcityList = provinceandcityDao.selectList(queryWrapper);
        List<ProvinceAndCityDto> provinceAndCityDtoList = MappingUtil.getSimpleMapperFacade().mapAsList(provinceandcityList, ProvinceAndCityDto.class);
        return provinceAndCityDtoList;
    }

    @Override
    public List<ProvinceAndCityDto> getAllWithParent() {
        QueryWrapper<Provinceandcity> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("DisplayOrder");
        List<Provinceandcity> provinceandcityList = provinceandcityDao.selectList(queryWrapper);
        List<ProvinceAndCityDto> provinceAndCityDtoList = MappingUtil.getSimpleMapperFacade().mapAsList(provinceandcityList, ProvinceAndCityDto.class);

        ProvinceAndCityDto zeroParent = new ProvinceAndCityDto();
        zeroParent.setProvinceAndCityGUID(UUID.randomUUID());
        zeroParent.setDisplayOrder(0);
        zeroParent.setItemName("全国");
        zeroParent.setNormName("全国");
        zeroParent.setParentCode("-1");
        zeroParent.setItemCode("0");
        provinceAndCityDtoList.add(0, zeroParent);
        return provinceAndCityDtoList;

    }

    @Override
    public List<ProvinceAndCityDto> getProvince(ProvinceAndCityConditionDto conditionDto) {


        QueryWrapper<Provinceandcity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ParentCode", "0");
        queryWrapper.orderByAsc("DisplayOrder");

        if (conditionDto != null && conditionDto.getProvinceCode() != null && conditionDto.getProvinceCode().size() > 0) {
            queryWrapper.in("itemCode", conditionDto.getProvinceCode());
        }

        List<Provinceandcity> provinceandcityList = provinceandcityDao.selectList(queryWrapper);
        List<ProvinceAndCityDto> provinceAndCityDtoList = MappingUtil.getSimpleMapperFacade().mapAsList(provinceandcityList, ProvinceAndCityDto.class);

        if (conditionDto != null && conditionDto.getIsIncludeNationwide()) {//增加"全国选项"
            ProvinceAndCityDto zeroParent = new ProvinceAndCityDto();
            zeroParent.setProvinceAndCityGUID(UUID.randomUUID());
            zeroParent.setDisplayOrder(0);
            zeroParent.setItemName("全国");
            zeroParent.setNormName("全国");
            zeroParent.setParentCode("-1");
            zeroParent.setItemCode("0");
            provinceAndCityDtoList.add(0, zeroParent);
        }
        return provinceAndCityDtoList;

    }

    public List<ProvinceAndCityDto> getCityByProvinceCode(List<String> codes) {
        QueryWrapper<Provinceandcity> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("DisplayOrder");
        if (codes.size() > 0) {
            queryWrapper.in("parentCode", codes);
        }
        List<Provinceandcity> provinceandcityList = provinceandcityDao.selectList(queryWrapper);
        List<ProvinceAndCityDto> provinceAndCityDtoList = MappingUtil.getSimpleMapperFacade().mapAsList(provinceandcityList, ProvinceAndCityDto.class);
        return provinceAndCityDtoList;
    }

    public List<ProvinceAndCityDto> getCityAndProvinceCode(List<String> provinceCodes) {
        List<ProvinceAndCityDto> provinceDtoList = new ArrayList<>();
        if (provinceCodes.size() == 0) {
            return provinceDtoList;
        }
        QueryWrapper<Provinceandcity> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("DisplayOrder");
        if (provinceCodes.size() > 0) {
            queryWrapper.in("parentCode", provinceCodes);
        }
        List<Provinceandcity> provinceandcityList = provinceandcityDao.selectList(queryWrapper);
        List<ProvinceAndCityDto> provinceAndCityDtoList = MappingUtil.getSimpleMapperFacade().mapAsList(provinceandcityList, ProvinceAndCityDto.class);

        QueryWrapper<Provinceandcity> queryWrapperProvince = new QueryWrapper<>();
        queryWrapperProvince.orderByAsc("DisplayOrder");
        queryWrapperProvince.in("ItemCode", provinceCodes);
        List<Provinceandcity> provinceList = provinceandcityDao.selectList(queryWrapperProvince);
        provinceDtoList = MappingUtil.getSimpleMapperFacade().mapAsList(provinceList, ProvinceAndCityDto.class);
        provinceDtoList.addAll(provinceAndCityDtoList);
        return provinceDtoList;
    }

}
